package indzcraft.ui;

import indzcraft.loader.Gem;
import indzcraft.loader.GemDB;
import indzcraft.loader.ItemEquiped;
import indzcraft.ranking.EP;
import indzcraft.ranking.ItemCompare;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;

public class GemPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private IndzCraftUI icui = null;
	private int slot;
	private String slotName;
	private int gemSlot;
	private String gemSlotColor;
	private ItemEquiped item;
	
	public GemPanel(IndzCraftUI ui,int slot,int gemSlot){
		super();
		this.icui = ui;
		this.slot = slot;
		this.slotName = Slot.possibleSlots[slot].getName();
		this.item = icui.getLoader().getEquipment().get(slotName);
		this.gemSlot = gemSlot;
		this.gemSlotColor = icui.getLoader().getEquipment().get(slotName).getGemSlot()[gemSlot];
		
		setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		
		
		// load gems
		GemDB gemDB = icui.getLoader().getGemDB();
		GemButton gb = null;
		ArrayList<Gem> items = new ArrayList<Gem>();
		for ( Gem elem : gemDB.getDB().values() ){
			if(gemSlotColor.equals("META") && elem.getType() == Gem.META)
				items.add(elem);
			else if(!gemSlotColor.equals("META") && elem.getType() != Gem.META)
				items.add(elem);
		}
		ItemCompare<Gem> ic = new ItemCompare<Gem>(icui,item,items);
		ArrayList<EP<Gem>> itemep = ic.getComperator();
		Iterator<EP<Gem>> it = itemep.iterator();
		double max = 0;
		int j = 0;
		Gem curGem = icui.getLoader().getEquipment().get(slotName).getGem(gemSlot);
		while (it.hasNext()) {
			Gem gem = it.next().getItem();
			if(max == 0) max = icui.calc.calcGemEP(gem);
			gb = new GemButton(gem,slot,gemSlot);
			
			c.anchor = GridBagConstraints.WEST;
            c.insets = new Insets(0, 0, 0, 7);
            c.gridx = 0; c.gridy = j; c.gridheight = 3; c.gridwidth = 1;
            if (gem == curGem){
            	gb.setSelected(true);
            }
            add(gb, c);
            
            JLabel jl = new JLabel();
            File file = new File("./icons/"+gem.getIcon()+".png");
            Image image;
			try {
				if(file.exists())
					image = ImageIO.read(file);
				else
					image = ImageIO.read(new File("./icons/inv_misc_questionmark.png"));
				image = createImage(new FilteredImageSource(image.getSource(),
						new CropImageFilter(5, 5, 54, 54)));
				jl.setIcon(new ImageIcon(image));
			} catch (IOException e) {
				e.printStackTrace();
			}
            c.insets = new Insets(0, 0, 0, 7);
            c.gridx = 1; c.gridy = j; c.gridheight = 3; c.gridwidth = 1;
            add(jl, c);
            
            String name = gem.getName();
            if (name.length()>35)
                    name = name.substring(0,32)+"...";
            JLabel label = new JLabel(name);
            c.insets = new Insets(0, 0, 0, 0);
            c.gridx = 2; c.gridy = j; c.gridheight = 1;
            add(label, c);
            
            String lvl = ""+gem.getType();
            label = new JLabel(String.format("Type: %s", lvl));
            c.gridx = 2; c.gridy = j+1;
            add(label, c);
            
            label = new JLabel(String.format("%.2f", icui.calc.calcGemEP(gem)));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j;
            add(label, c);
            
            double diff;
            if (curGem != null)
                diff = icui.calc.calcGemEP(gem) - icui.calc.calcGemEP(curGem);
            else
                diff = icui.calc.calcGemEP(gem);
            label = new JLabel(String.format("%+.2f", diff));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j+1;
            add(label, c);
            
            // bar
            c.insets = new Insets(5, 0, 0, 0);
            c.gridx = 2; c.gridy = j+2; c.gridwidth = 2;
            add(new RatingPanel(icui.calc.calcGemEP(gem), (float) max), c);
            
            if (it.hasNext()) {
                JSeparator sep = new JSeparator();
                c.insets = new Insets(3, 0, 3, 0);
                c.gridx = 1; c.gridy = j+3; c.gridwidth = 3;
                add(sep, c);
                j += 1;
            }
            
            j += 3;
		}
		
        setVisible( true );
	}
	
	private class GemButton extends JRadioButton implements ActionListener {
        
		private static final long serialVersionUID = 1L;
		private int slot;
		private int gemSlot;
		private Gem gem;
        
        public GemButton(Gem gem,int slot,int gemSlot) {
            this.gem = gem;
        	this.slot = slot;
        	this.gemSlot = gemSlot;
        	
            setFocusable(false);
            addActionListener(this);
            setVisible(true);
        }

        public void actionPerformed(ActionEvent e) {
            icui.setGemSlot(slot, gemSlot, gem);
            icui.showGemPanel(slot, gemSlot);
            icui.showStats();
        }

	}
}
